package zack.project.infrastructure.event;

import zack.project.types.event.BaseEvent;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author A1793
 */
@Slf4j
@Component
public class EventPublisher {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void publish(String topic, BaseEvent.EventMessage<?> message) {
        String jsonMessage = JSON.toJSONString(message);
       try{

           rabbitTemplate.convertAndSend(topic, jsonMessage);
           log.info("发送MQ消息 topic:{} message:{}", topic, jsonMessage);

       }catch(Exception e){
           log.error("发送MQ消息失败 topic:{} message:{}", topic, jsonMessage, e);
       }


    }
    public void publish(String topic, String jsonMessage) {
        try{

            rabbitTemplate.convertAndSend(topic, jsonMessage);
            log.info("发送MQ消息 topic:{} message:{}", topic, jsonMessage);

        }catch(Exception e){
            log.error("发送MQ消息失败 topic:{} message:{}", topic, jsonMessage, e);
        }

    }


}
